<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ include file="/include/taglibs.jsp"%>
<html>
<head>
	<title>分配功能</title>
	<%@ include file="/include/openstyles.jsp"%>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/ztree/js/jquery.ztree.core-3.5.js" ></script>		<!-- 树插件核心 -->
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/ztree/js/jquery.ztree.exedit-3.5.js" ></script>		<!-- 树编辑插件 -->
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/ztree/js/jquery.ztree.excheck-3.5.js" ></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/input.js"></script>
	<script type="text/javascript" src="${ctx}/assets/uc/js/common.js"></script>
</head>
<body>
	<div class="row">
		<div class="col-xs-12">
			<div class="row xtgltree">
				<div class="widget-box widget-color-blue2" style="height:500px;min-height:420px; height:auto!important">
					<div class="widget-header">
						<h4 class="widget-title lighter smaller">功能列表</h4>
					</div>
					<div class="widget-body">
						<div class="widget-main padding-8">
							<div class="zTreeDemoBackground">
								<ul id="treeDemo" class="ztree"></ul>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		/**
		 * 设置全局变量
		 */
		var tree ="treeDemo";//树id
		var $tree=$("#treeDemo");
		var zTree; //树对象
		jQuery(function($){
			var setting = {
				async:{
					autoParam: ["id=id"],
					enable:true,
					type:"GET",
					dataType:'json',
					url:"${ctx}/${frameworkPath}/role/function/${param.role}"
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				check: {
					enable: true,
					chkStyle:'checkbox'
				},
				callback: {
					onAsyncSuccess: onAsyncSuccess	//加载完成后回调
				}
			};
			zTree=$.fn.zTree.init($tree, setting); //树初始化

			/**
			 * 数据加载完成后触发事件
			 */
			function onAsyncSuccess(){
				treeOnAsyncSuccess();//数据加载完成后触发事件
			}

		});

		/**
		 * 数据加载完成后触发事件
		 */
		var treeOnAsyncSuccess=function(){

			asyncNodes(zTree.getNodes());
			zTree.expandAll(true);
		}

		function asyncNodes(nodes) {
			if (!nodes) return;
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			for (var i=0, l=nodes.length; i<l; i++) {
				if (nodes[i].isParent && nodes[i].zAsync) {
					asyncNodes(nodes[i].children);
				}
				else {
					zTree.reAsyncChildNodes(nodes[i], "refresh", true);
				}
			}
		}

		function submit(){
			var retObj =new Object();

			var funs=zTree.getCheckedNodes(true);
			var arrFuns = new Array();
			$.each(funs, function(i,val){
				arrFuns.push(val.id);
			});
			$.ajax({
				type : 'POST',
				url : '${ctx}/${frameworkPath}/role/function',
				traditional : true,
				async :false,
				dataType:'json',
				data : {'checked' : arrFuns,role:'${param.role}'},
				success : function(data) {
					if (data.success) {
						retObj.text ="保存成功！";
						retObj.state = true;
					} else {
						retObj.text = text;
						retObj.state =false;
					}
				},
				error : function(XmlHttpRequest, textStatus, errorThrown) {
					retObj.text = "保存失败";
					retObj.state =false;
				}
			});
			return retObj;
		}
	</script>
</body>
</html>